#define SEG_A 2  //Arduino Pin2--->SegLed Pin11
#define SEG_B 3  //Arduino Pin3--->SegLed Pin7
#define SEG_C 4  //Arduino Pin4--->SegLed Pin4
#define SEG_D 5  //Arduino Pin5--->SegLed Pin2
#define SEG_E 6  //Arduino Pin6--->SegLed Pin1
#define SEG_F 7  //Arduino Pin7--->SegLed Pin10
#define SEG_G 8  //Arduino Pin8--->SegLed Pin5   
#define SEG_H 9  //Arduino Pin9--->SegLed Pin3

#define COM1 10  //Arduino Pin10--->SegLed Pin12
#define COM2 11  //Arduino Pin11--->SegLed Pin9
#define COM3 12  //Arduino Pin12--->SegLed Pin8
#define COM4 13  //Arduino Pin13--->SegLed Pin6

#define LED 8
#define SEGLENG 12
unsigned short i=0;  //计数
int j = 1;  //循环

const unsigned int segname[] = {SEG_A,SEG_B,SEG_C,SEG_D,SEG_E,SEG_F,SEG_G,SEG_H,COM1,COM2,COM3,COM4}; //LED与COM名字数组
const unsigned char code[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};//共阴段码0-9,.


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("测试全亮是否正常");
  for(int i=0; i < SEGLENG; i++)
  {
     pinMode(segname[i],OUTPUT);
     if(i >= 8){
         digitalWrite(segname[i],HIGH);   //将四个数码管全部点亮  //选通位选
      }
  }
  //数码管内部的8个LED为（LOW 点亮）
  for(int i = 1; i < LED; i++)
  {
    digitalWrite(segname[i],LOW);
   }
   //测试10秒后熄灭四个数码管
 //  delay(10000);
   for(int i = 8; i < 12; i++)
   {
          digitalWrite(segname[i],LOW); 
   }
   Serial.println("初始化完成");
}

//方式二：让4个数码管依次显示0-9
void Display(unsigned char value)
{
   for(int j = 0; j < LED; j++)
   {
      digitalWrite(segname[j],!bitRead(value,j));  //任然取反由于low点亮LED
   }
}
void shownum(int num)
{
  //判断第几位数码管显示
  switch(num)
  {
    case 1:
           digitalWrite(segname[8],HIGH); 
           digitalWrite(segname[9],LOW); 
           digitalWrite(segname[10],LOW); 
           digitalWrite(segname[11],LOW);         break;

    case 2:
           digitalWrite(segname[8],LOW); 
           digitalWrite(segname[9],HIGH); 
           digitalWrite(segname[10],LOW); 
           digitalWrite(segname[11],LOW);         break;

    case 3:
           digitalWrite(segname[8],LOW); 
           digitalWrite(segname[9],LOW); 
           digitalWrite(segname[10],HIGH); 
           digitalWrite(segname[11],LOW);         break;

    case 4:
           digitalWrite(segname[8],LOW); 
           digitalWrite(segname[9],LOW); 
           digitalWrite(segname[10],LOW); 
           digitalWrite(segname[11],HIGH);         break;
    default:break;
  }
   for(int i = 0; i < 10; i++)
  {
    Serial.println(i);
    Display(code[i]);   
    delay(1000);
  }
  delay(50);
 for(int j = 0; j < LED; j++)
 {
    digitalWrite(segname[j],HIGH);  //熄灭
  }
}


void loop() {
  // put your main code here, to run repeatedly:
   i=i+j;
   shownum(i);
   delay(1000);
   if( i >= 4 || i <= 1){
     j = -j;
   }
}
